package com.gem.controller;

import com.gem.domain.CDailySpending;
import com.gem.domain.User;
import com.gem.service.CDailySpendingService;
import com.gem.service.UserService;
import com.gem.util.ConditionUtil;
import com.gem.util.JSONResultUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 日常经费控制器
 * create 2019-10-19 19:11
 * Modified By:
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/ds")
public class CDailySpendingController {

    //自动注入ConditionUtil
    private ConditionUtil cu;

    @Autowired
    public void setCu(ConditionUtil cu) {
        this.cu = cu;
    }

    //自动注入JSONResultUtil
    private JSONResultUtil result;

    @Autowired
    public void setResult(JSONResultUtil result) {
        this.result = result;
    }

    //自动注入CDailySpendingService
    private CDailySpendingService cDailySpendingService;

    @Autowired
    public void setcDailySpendingService(CDailySpendingService cDailySpendingService) {
        this.cDailySpendingService = cDailySpendingService;
    }

    //自动注入UserService
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //首页-浏览所有
    @RequestMapping("/dsShow")
    public String dsShow(HttpSession session, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            PageInfo<CDailySpending> pageBean = cDailySpendingService.selectDSByPage(currentPage, pageSize, session);
            if (pageBean != null && pageBean.getList().size() > 0) {
                model.addAttribute("pageBean", pageBean);
                List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("pageSize", pageSize);
            return "Training/DailySpending";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/home", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //添加页面
    @RequestMapping("/dsAddUI")
    public String dsAddUI(Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            List<User> userList = userService.findAllUser();
            if (userList.size() > 0) {
                model.addAttribute("sellerList", userList);
                model.addAttribute("currentPage", currentPage);
                model.addAttribute("pageSize", pageSize);
                return "Training/DailySpending_insert";
            } else {
                MessageUtil.returnInfoPage("3", "/ds/dsShow", "暂无渠道专员信息哦！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/ds/dsShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //添加功能
    @RequestMapping("/dsAdd")
    public String dsAdd(CDailySpending cDailySpending, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            if (cDailySpendingService.addDSService(cDailySpending)) {
                MessageUtil.returnInfoPage("3", "/ds/dsShow?currentPage=" + currentPage + "&pageSize=" + pageSize, "添加成功！", model);
            } else {
                MessageUtil.returnInfoPage("3", "/ds/dsShow?currentPage=" + currentPage + "&pageSize=" + pageSize, "添加失败了哟！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/ds/dsShow", "服务器异常，请联系管理员哦！", model);
        }
        return "message";
    }

    //修改页面
    @RequestMapping("/dsModifyUI")
    public String dsModifyUI(String dsId,String cdscDate, String cdscSeller, String cdscPeople, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            CDailySpending cDailySpending = cDailySpendingService.selectDSByIdService(Integer.parseInt(dsId));
            List<User> userList = userService.findAllUser();
            if (cDailySpending != null && userList.size() > 0) {
                model.addAttribute("ds", cDailySpending);
                model.addAttribute("sellerList", userList);
                model.addAttribute("currentPage", currentPage);
                model.addAttribute("pageSize", pageSize);
                model.addAttribute("cdscDate",cdscDate);
                model.addAttribute("cdscSeller",cdscSeller);
                model.addAttribute("cdscPeople",cdscPeople);
                return "Training/DailySpending_modify";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/ds/dsShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //修改功能
    @RequestMapping(value = "/dsModify", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil dsModify(CDailySpending cDailySpending) {
        try {
            if (cDailySpendingService.modifyDSService(cDailySpending)) {
                result.setId(0);
                result.setData("修改成功！");
            } else {
                result.setId(1);
                result.setData("修改未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //单个删除
    @RequestMapping(value = "/dsDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil dsDelete(String dsId, int currentPageSize, int currentPage, int lastPage) {
        try {
            if (dsId != null && !dsId.trim().equals("")) {
                int ds_Id = Integer.parseInt(dsId);
                if (cDailySpendingService.deleteDSByIdService(ds_Id)) {
                    if (currentPageSize == 1 && currentPage == lastPage) {
                        result.setId(0);
                    } else {
                        result.setId(1);
                    }
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("服务器异常，删除失败哦！");
                }
            } else {
                result.setId(-1);
                result.setData("服务器异常，没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //批量删除
    @RequestMapping(value = "/dssDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil dssDelete(String dsIdStr, int currentPageSize, int currentPage, int lastPage) {
        try {
            String[] dsIdArr = dsIdStr.split(",");
            if (dsIdArr.length != 0) {
                if (cDailySpendingService.deleteDSByIdsService(dsIdArr)) {
                    if (dsIdArr.length == currentPageSize && currentPage == lastPage) {
                        result.setId(0);
                    } else {
                        result.setId(1);
                    }
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("删除发生异常，请检查服务器！");
                }
            } else {
                result.setId(-1);
                result.setData("没有提交游戏编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //查询显示
    @RequestMapping("/dsSearchShow")
    public String dsSearch(HttpSession session, String cdscDate, String cdscSeller, String cdscPeople, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            List<User> userList = userService.selectAllByPersonName('%'+cdscSeller+'%');
            if (userList.size() > 0) {
                Integer[] sellerUidArr = new Integer[userList.size()];
                for (int i = 0; i < userList.size(); i++) {
                    sellerUidArr[i] = userList.get(i).getUid();
                }
                cu.setCdscSellerArr(sellerUidArr);
            }
            cu.setCdscDate(cdscDate);
            cu.setCdscPeople(cdscPeople);
            if (!(cdscSeller != null && userList.size() == 0)) {
                PageInfo<CDailySpending> pageBean = cDailySpendingService.selectDSByConditionByPage(currentPage, pageSize, cu, session);
                if (pageBean != null && pageBean.getList().size() > 0 && pageBean.getPages() > 0 && pageBean.getTotal() > 0) {
                    List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(), currentPage);
                    model.addAttribute("pageValue", pageValue);
                    model.addAttribute("pageBean", pageBean);
                } else {
                    model.addAttribute("info", "暂无信息记录");
                }
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            cu.setCdscSeller(cdscSeller);
            model.addAttribute("cdsc", cu);
            model.addAttribute("pageSize", pageSize);
            model.addAttribute("searchURL", "/ds/dsSearchShow?" + "cdscDate=" + cdscDate + "&cdscSeller=" + cdscSeller + "&cdscPeople=" + cdscPeople);
            return "Training/DailySpending";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/ds/dsShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

}
